// 视图
import * as Cesium from 'cesium'
import { heightToZoom } from './transform'

export default {
  /**
   * @function 地理信息显示
   * @param viewer
   * @param callback
  */
  syncViewInfo (viewer, callback) {
    callback = typeof callback === 'function' ? callback : function () {}
    // 地理信息显示
    var ellipsoid = viewer.scene.globe.ellipsoid
    var canvas = viewer.scene.canvas
    var handler = new Cesium.ScreenSpaceEventHandler(canvas)
    handler.setInputAction((movement) => {
      var cartesian = viewer.camera.pickEllipsoid(movement.endPosition, ellipsoid)
      if (cartesian) {
        var cartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian)
        var lat = Cesium.Math.toDegrees(cartographic.latitude).toFixed(4)
        var lng = Cesium.Math.toDegrees(cartographic.longitude).toFixed(4)
        var height = (viewer.camera.positionCartographic.height / 1000).toFixed(3)
        var heading = Cesium.Math.toDegrees(viewer.camera.heading).toFixed(2)
        var pitch = Cesium.Math.toDegrees(viewer.camera.pitch).toFixed(2)
        var roll = Cesium.Math.toDegrees(viewer.camera.roll).toFixed(2)
        var zoom = heightToZoom(viewer.camera.positionCartographic.height)
        // const level = viewer.scene.pickPosition(movement.clientX, movement.clientY).height;
        // 经度：lng
        // 纬度：lat
        // 视角高：height(km)
        // 方向：heading(°)
        // 俯仰角：pitch(°)
        callback({ lng, lat, height, heading, pitch, roll, zoom })
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    handler.setInputAction(function() {
      var height = (viewer.camera.positionCartographic.height / 1000).toFixed(2)
      callback({ height })
    }, Cesium.ScreenSpaceEventType.WHEEL)
  }
}